package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.access.EntityPersonnelYkm;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.vo.recode.DoorRecodeVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoPersonnelYkm;
import com.ocom.srvpaccess.service.PersonnelInfoService;
import com.ocom.srvpaccess.service.PersonnelYkmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PersonnelYkmServiceImpl extends ServiceImpl<IDaoPersonnelYkm, EntityPersonnelYkm> implements PersonnelYkmService {

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @Autowired
    private IDaoPersonnelYkm personnelYkm;

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
    public Boolean add(Long companyId, DoorRecodeVo doorRecodeVo) {
        if(doorRecodeVo.getPId()==null || doorRecodeVo.getPId()==0){
            return false;
        }
        if(doorRecodeVo.getData()!=null && doorRecodeVo.getData().length()>18){
            UpdateWrapper<EntityPersonnelInfo> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("p_id",doorRecodeVo.getPId());
            updateWrapper.set("ykm_str",doorRecodeVo.getData());
            personnelInfoService.update(updateWrapper);
        }

        QueryWrapper<EntityPersonnelYkm> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("p_id",doorRecodeVo.getPId());
        queryWrapper.eq("com_id",doorRecodeVo.getComId());
        queryWrapper.last("limit 1");
        EntityPersonnelYkm r=this.getOne(queryWrapper);
        if(r==null){
            EntityPersonnelYkm entityPersonnelYkm=new EntityPersonnelYkm();
            entityPersonnelYkm.setPId(doorRecodeVo.getPId().longValue());
            entityPersonnelYkm.setComId(doorRecodeVo.getComId().longValue());
            entityPersonnelYkm.setDeviceNo(doorRecodeVo.getDeviceNo());
            entityPersonnelYkm.setHsResult(doorRecodeVo.getHsResult());
            entityPersonnelYkm.setHsTime(DateUtils.strToDate(doorRecodeVo.getHsTime()));
            entityPersonnelYkm.setYkmInfo(doorRecodeVo.getData());
            entityPersonnelYkm.setCodeColor(doorRecodeVo.getCodeColor());
            entityPersonnelYkm.setColorTime(DateUtils.strToDate(doorRecodeVo.getColorTime()));
            entityPersonnelYkm.setYmNum(doorRecodeVo.getYmNum());
            entityPersonnelYkm.setYmTime(DateUtils.strToDate(doorRecodeVo.getYmTime()));
            entityPersonnelYkm.setReason("");
            entityPersonnelYkm.setCreateTime(new Date());
            return this.save(entityPersonnelYkm);
        }else{
            UpdateWrapper<EntityPersonnelYkm> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("p_id",doorRecodeVo.getPId());
            updateWrapper.eq("com_id",doorRecodeVo.getComId());

            updateWrapper.set("device_no",doorRecodeVo.getDeviceNo());
            updateWrapper.set("ykm_info",doorRecodeVo.getData());
            updateWrapper.set("code_color",doorRecodeVo.getCodeColor());
            updateWrapper.set("color_time",DateUtils.strToDate(doorRecodeVo.getColorTime()));
            updateWrapper.set("hs_result",doorRecodeVo.getHsResult());
            updateWrapper.set("hs_time",DateUtils.strToDate(doorRecodeVo.getHsTime()));
            updateWrapper.set("ym_num",doorRecodeVo.getYmNum());
            updateWrapper.set("ym_time",DateUtils.strToDate(doorRecodeVo.getYmTime()));
            updateWrapper.set("reason","");
            updateWrapper.set("update_time",new Date());
            return this.update(updateWrapper);
        }
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityPersonnelYkm selectOne(Long comId, Integer pId) {
        QueryWrapper<EntityPersonnelYkm> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("p_id",pId);
        return personnelYkm.selectOne(query);
    }


}
